package org.liuwei.concucrent.base03.RentrantLock02;

import java.lang.management.ManagementFactory;
import java.lang.management.ThreadInfo;
/**
 * 
* @Title: DeadLock.java 
* @Package org.liuwei.concucrent.base03.ReentrantLock01 
* @Description: TODO(死锁检测) 
* @author 刘伟  15818570028@163.com   
* @date 2016年8月20日 下午8:06:09 
* @version V1.0
 */
public class DeadLock {
	private final static java.lang.management.ThreadMXBean mbean = ManagementFactory
			.getThreadMXBean();
	final static Runnable deadLockCheck = new Runnable() {

		public void run() {
			while (true) {
				long[] deadlockIds = mbean.findDeadlockedThreads();
				if (deadlockIds != null) {
					ThreadInfo[] threadInfos = mbean.getThreadInfo(deadlockIds);
					for (Thread t : Thread.getAllStackTraces().keySet()) {
						for (int i = 0; i < threadInfos.length; i++) {
							if (t.getId() == threadInfos[i].getThreadId())
								t.interrupt();
						}
					}
				}

			}

		}
	};

	public static void checkAndInterrupted() {
		Thread t = new Thread(deadLockCheck);
		t.setDaemon(true);
		t.start();
	}
}
